package com.appkubes.portal.admin.application.app.menu.controller;


import com.appkubes.base.core.entity.vo.AppKeyVO;
import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.portal.admin.application.app.menu.entity.vo.MenuRoleGetVO;
import com.appkubes.portal.admin.application.app.menu.entity.vo.MenuRoleSetVO;
import com.appkubes.portal.admin.application.app.menu.entity.vo.MenuRoleSingleSetVO;
import com.appkubes.portal.admin.application.app.role.entity.dto.RoleMenuDTO;
import com.appkubes.portal.admin.application.auth.service.KeyCloakRoleService;
import com.appkubes.portal.admin.facility.util.ArrayHandel;
import com.appkubes.portal.core.controller.BasePortalAppMenuRoleController;
import com.appkubes.portal.core.entity.PortalAppMenuRole;
import com.appkubes.portal.core.service.BasePortalAppMenuRoleService;
import com.appkubes.portal.core.service.BasePortalAppRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "角色菜单模块")
@RestController
@RequestMapping("/app/menu/role")
public class AppMenuRoleController extends BasePortalAppMenuRoleController {

    @Resource
    BasePortalAppRoleService basePortalAppRoleService;

    @Resource
    BasePortalAppMenuRoleService basePortalAppMenuRoleService;

    @Resource
    KeyCloakRoleService keyCloakRoleService;

    @ApiOperation("设置权限角色")
    @PostMapping("/set")
    public Response set(@ParamValid MenuRoleSetVO menuRoleSetVO) {
        PortalAppMenuRole appMenuRole = new PortalAppMenuRole();
        BeanUtils.copyProperties(menuRoleSetVO, appMenuRole);

        List<PortalAppMenuRole> pageResult = this.selectListByEntity(appMenuRole);
        List<String> roleList = new ArrayList<>();
        for (PortalAppMenuRole role : pageResult) {
            roleList.add(role.getRoleCode());
        }
        String[] roles = new String[roleList.size()];
        roleList.toArray(roles);

        String[] delMenus = ArrayHandel.getDelete(roles, menuRoleSetVO.getMenuKey());
        String[] addMenus = ArrayHandel.getAdd(roles, menuRoleSetVO.getMenuKey());

        for (String menuKey : delMenus) {
            PortalAppMenuRole portalAppMenuRole = new PortalAppMenuRole();
            portalAppMenuRole.setMenuKey(menuKey);
            portalAppMenuRole.setRoleCode(menuRoleSetVO.getRoleCode());
            this.removeByEntity(portalAppMenuRole);
        }

        for (String menuKey : addMenus) {
            PortalAppMenuRole appAdmin = new PortalAppMenuRole();
//            appAdmin.setAppKey(menuRoleSetVO.getAppKey());
            appAdmin.setMenuKey(menuKey);
            appAdmin.setRoleCode(menuRoleSetVO.getRoleCode());
            appAdmin.setStatus("ACTIVE");
            this.getDao().insert(appAdmin);
        }

        return Response.success();
    }

    @ApiOperation("设置单个权限角色")
    @PostMapping("/singleSet")
    public Response singleSet(@ParamValid MenuRoleSingleSetVO menuRoleSetVO) {
        PortalAppMenuRole appAdmin = new PortalAppMenuRole();
        BeanUtils.copyProperties(menuRoleSetVO, appAdmin);
        QueryWrapper<PortalAppMenuRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_key",menuRoleSetVO.getMenuKey());
        queryWrapper.eq("role_code",menuRoleSetVO.getRoleCode());
        PortalAppMenuRole portalAppMenuRole = basePortalAppMenuRoleService.getOne(queryWrapper);
        if (portalAppMenuRole != null) {
            appAdmin.setMenuRoleKey(portalAppMenuRole.getMenuRoleKey());
            appAdmin.setStatus(menuRoleSetVO.getStatus());
        }
        return Response.success(this.saveOrUpdate(appAdmin));
    }

    @ApiOperation("获取角色权限")
    @PostMapping("/getRoleMenu")
    public Response getRoleMenu(@ParamValid MenuRoleGetVO menuRoleGetVO) {
        PortalAppMenuRole appMenuRole = new PortalAppMenuRole();
        BeanUtils.copyProperties(menuRoleGetVO, appMenuRole);
        appMenuRole.setStatus("ACTIVE");
        List<PortalAppMenuRole> pageResult = this.selectListByEntity(appMenuRole);
        List<String> menuList = new ArrayList<>();
        for (PortalAppMenuRole role : pageResult) {
            menuList.add(role.getMenuKey());
        }
        String[] roles = new String[menuList.size()];
        menuList.toArray(roles);
        return Response.success(roles);
    }

    @ApiOperation("获取所有角色权限")
    @PostMapping("/getAllRoleMenu")
    public Response getAllRoleMenu(@ParamValid AppKeyVO appKeyVO) {
        List<RoleRepresentation> portalAppRoles = keyCloakRoleService.getClientRoleList();
        List<RoleMenuDTO> roleMenus = new ArrayList<>();
        for (RoleRepresentation role : portalAppRoles) {
            QueryWrapper<PortalAppMenuRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_code",role.getName());
            queryWrapper.eq("status","ACTIVE");
            List<PortalAppMenuRole> appMenuRoles = basePortalAppMenuRoleService.list(queryWrapper);
            roleMenus.add(new RoleMenuDTO(role, getMenus(appMenuRoles)));
        }
        return Response.success(roleMenus);
    }

    private String[] getMenus(List<PortalAppMenuRole> pageResult) {
        List<String> menuList = new ArrayList<>();
        for (PortalAppMenuRole role : pageResult) {
            menuList.add(role.getMenuKey());
        }
        String[] roles = new String[menuList.size()];
        menuList.toArray(roles);
        return roles;
    }
}
